package com.csdj.x315springboot.controller;


import com.csdj.x315springboot.common.ErrorCodeConstant;
import com.csdj.x315springboot.common.WebResult;
import com.csdj.x315springboot.entity.User;
import com.csdj.x315springboot.service.IUserService;
import com.csdj.x315springboot.util.RequestUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cloud
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @PostMapping("/login")
    public WebResult login(@RequestParam String username,
                           @RequestParam String password){
        User user = userService.login(username, password);
        if(user == null){
            return new WebResult(ErrorCodeConstant.U_OR_PWD_ERROR);
        }
        return new WebResult(ErrorCodeConstant.SUCCESS,user);

    }

    /**
     * 获取用户的信息
     * @param request
     * @return
     */
    @GetMapping("/info")
    public WebResult getUserInfo(HttpServletRequest request){
        User user = userService.getById(RequestUtil.getUserId(request));
        return new WebResult(ErrorCodeConstant.SUCCESS,user);
    }

}
